package com.healthdata.controller;

import com.healthdata.entity.DataRow;
import com.healthdata.service.ExportService;
import com.healthdata.service.HealthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    @Autowired
    private HealthDataService healthDataService;

    /**
     * 导出所有健康数据
     */
    @GetMapping("/all")
    public ResponseEntity<byte[]> exportAllData(
            @RequestParam String format,
            @RequestParam(required = false) String filename) {

        List<DataRow> data = healthDataService.getAllData();
        filename = filename != null ? filename : "all_health_data";

        // 确保文件名包含正确的扩展名
        filename = addExtensionIfMissing(filename, format);

        return exportService.exportData(format, data, filename);
    }

    /**
     * 按条件导出健康数据
     */
    @GetMapping("/filter")
    public ResponseEntity<byte[]> exportFilteredData(
            @RequestParam String format,
            @RequestParam(required = false) String filename,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String dataType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {

        List<DataRow> data = healthDataService.getFilteredData(userId, dataType, startDate, endDate);
        filename = filename != null ? filename : "filtered_health_data";

        // 确保文件名包含正确的扩展名
        filename = addExtensionIfMissing(filename, format);

        return exportService.exportData(format, data, filename);
    }

    /**
     * 导出分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<byte[]> exportPageData(
            @RequestParam String format,
            @RequestParam(required = false) String filename,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String sortBy,
            @RequestParam(defaultValue = "asc") String direction) {

        Page<DataRow> pageData = healthDataService.getPaginatedData(page, size, sortBy, direction);
        filename = filename != null ? filename : "paginated_health_data";

        // 确保文件名包含正确的扩展名
        filename = addExtensionIfMissing(filename, format);

        return exportService.exportPageData(format, pageData, filename);
    }

    /**
     * 根据格式添加正确的文件扩展名
     */
    @GetMapping("/table")
    public ResponseEntity<byte[]> exportTableData(
            @RequestParam String format,
            @RequestParam(required = false) String filename,
            @RequestParam String tableName) {

        List<DataRow> data = healthDataService.getDataByTableName(tableName);
        filename = filename != null ? filename : tableName + "_data";

        // 确保文件名包含正确的扩展名
        filename = addExtensionIfMissing(filename, format);

        return exportService.exportTableData(format, data, filename);
    }

    /**
     * 根据格式添加正确的文件扩展名
     */
    private String addExtensionIfMissing(String filename, String format) {
        if (format.equalsIgnoreCase("excel") || format.equalsIgnoreCase("xlsx")) {
            if (!filename.toLowerCase().endsWith(".xlsx")) {
                filename += ".xlsx";
            }
        } else if (format.equalsIgnoreCase("csv")) {
            if (!filename.toLowerCase().endsWith(".csv")) {
                filename += ".csv";
            }
        }
        return filename;
    }
}